<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use app\portal\model\PortalCategoryPost;
use app\portal\model\PortalPostModel;
use cmf\controller\HomeBaseController;
use app\portal\model\PortalCategoryModel;
use think\Db;

class ListController extends HomeBaseController
{
    public function index()
    {
        $id                  = $this->request->param('id', 0, 'intval');
        $portalCategoryModel = new PortalCategoryModel();

        $category = $portalCategoryModel->where('id', $id)->where('status', 1)->find();
       
        $this->assign('category', $category);

        $listTpl = empty($category['list_tpl']) ? 'list' : $category['list_tpl'];

        return $this->fetch('/' . $listTpl);
    }
    public function ajaxList(){
        $id                  = $this->request->param('id', 0, 'intval');
        $p = $this->request->param('p',1,'intval');
        $l = $this->request->param('l',10,'intval');
        $html = $this->request->param('html',1,'intval');
        $post_ids = Db::table('cmf_portal_category_post')->where('category_id',$id)->column('post_id');

        $portalPostModel = new PortalPostModel();
        $where = [
            'published_time' => [['< time', time()], ['> time', 0]],
            'post_status'    => 1,
            'delete_time'    => 0,
        ];

        if (!empty($param['keyword'])) {
            $where['post_title'] = ['like', "%{$param['keyword']}%"];
        }
        if($html==1){
            $portalCategoryModel = new PortalCategoryModel();

            $category = $portalCategoryModel->where('id', $id)->where('status', 1)->find();

            $listTpl = empty($category['list_tpl']) ? 'list' : $category['list_tpl'];
            $items = $portalPostModel->where($where)->whereIn('id',$post_ids)->order('create_time','DESC')->page($p,$l)->select();
            if(!$items){
                return '';
            }
            $this->assign('items',$items);
            $this->assign('cat_id',$id);
            return $this->fetch('/' . $listTpl.'-ajax');

        }

        //返回的数据必须是数据集或数组,item里必须包括id,name,如果想表示层级关系请加上 parent_id
        return $portalPostModel->where($where)->whereIn('id',$post_ids)->order('create_time','DESC')->page($p,$l)->select();
    }
}
